#include "apue.h"

static volatile sig_atomic_t quitflag; /* set nonzero by signal handler */

static void sig_int(int signo);

int main(void) {
    sigset_t newmask, oldmask, zeromask;
    sigemptyset(&newmask);
    sigemptyset(&zeromask);

    sigaddset(&newmask, SIGQUIT);

    if (signal(SIGINT, sig_int) == SIG_ERR)  
        err_sys("signal(SIGINT) error");
    if (signal(SIGQUIT, sig_int) == SIG_ERR)
        err_sys("signal(SIGQUIT) error");

    /**
     * Block SIGQUIT and save current signal mask
     */
    if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
        err_sys("SIG_BLOCK error");
    }

    while (quitflag == 0)
        sigsuspend(&zeromask);

    /**
     * SIGQUIT has been caught and is now blocked; do whatever.
     */
    quitflag= 0;
    /**
     * Reset signal mask which unblocks SIGQUIT.
     */
    if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
        err_sys("SIG_SETMASK error");
    }
    exit(0);
}

static void sig_int(int signo) {
    if (signo == SIGINT) {
        printf("\ninterrupt\n");
    } else {
        quitflag= 1;    /* set flag for main loop */
    }
}

